package com.translate.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.common.util.ArrayUtil;
import com.common.util.CopyEntity;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.CustomerPoolMapper;
import com.translate.entity.CustomerPool;
import com.page.PageResponseData;
import com.translate.web.param.contract.ICustomerSearchParam;
import com.param.contract.IIdParam;
import com.translate.web.service.ICustomerSeaService;
import com.basic.service.impl.BaseServiceImpl;


@Service
public class CustomerSeaServiceImpl extends BaseServiceImpl<CustomerPool> implements ICustomerSeaService {

	public CustomerSeaServiceImpl(CustomerPoolMapper mapper) {
		super(mapper);
		// TODO Auto-generated constructor stub
	}

	private RedisTemplate<String, Object> redisTemplate;


	@Autowired
	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}


	@Override
	protected void insertPropteris(CustomerPool data) {
		
		long time = System.currentTimeMillis() / 1000;
		
		data.setCreateTime(time);
		
		data.setUpdateTime(time);
	}


	@Override
	protected void updatePropteris(CustomerPool data) {
		// TODO Auto-generated method stub
		data.setUpdateTime(System.currentTimeMillis() / 1000);
		
	}

	
	protected String getTableNameNoFrontSuffix() {

		return  "_customer_pool";
	}

	@Override
	protected Class<CustomerPool> getEntityClass() {
		// TODO Auto-generated method stub
		return CustomerPool.class;
	}


	protected int getId(CustomerPool data) {
		return data.getId();
	}


	@Override
	public <VO, R> List<R> listToCacheByCondition(List<VO> list, Function<VO, Integer> function, Class<R> target) {

		
		List<CustomerPool> CustomerPools = this.listToCache(list, function);
		
		return entityConvertToVo(CustomerPools, target);
	}
	
	protected <VO> void editVO(VO vo, CustomerPool t) {
			
//		ICustomerPoolFollowPersonVO CustomerPoolFollowPersonVO = (ICustomerPoolFollowPersonVO)vo;
//	
//		
//		CustomerPoolFollowPersonVO.setCustomerPoolId(t.getId());
		
	}
	
	/**
	 * 缓存
	 * @param <S>
	 * @param list
	 * @param function
	 * @return
	 */
	protected <VO> List<CustomerPool> listToCache(List<VO> list, Function<VO, Integer> function) {
		
		Integer[] integers = getIds(list, function);
		
		String key = RedisConstant.getCustomerKey(integers);
		
		ValueOperations<String, Object> operations = this.redisTemplate.opsForValue();
		
		List<CustomerPool>  CustomerPools= (List<CustomerPool>) operations.get(key);
		
		if (null != CustomerPools) {

			return CustomerPools;
		}
		
		CustomerPools = this.getDataByIds(integers);
		
		if ( null == CustomerPools ) {
			CustomerPools = new ArrayList<>(0);
			
			operations.set(key, CustomerPools, 3, TimeUnit.SECONDS);
			
			return CustomerPools;
		}
		
		operations.set(key, CustomerPools, 6, TimeUnit.SECONDS);
		
		return CustomerPools;
	}


	@Override
	public <VO, R> List<R> getCustomer(List<VO> list, Function<VO, Integer> function, Class<R> target) {
		return null;
	}
	
	

	/**
	 * 获取客户列表
	 */
	@Override
	public <VO, R> PageResponseData<R> getCustomer(List<VO> list, Function<VO, Integer> function,
			Class<R> target,
			ICustomerSearchParam CustomerPoolSearchParam) {
		
		if (null == CustomerPoolSearchParam.getParam()) {
			return null;
		}
		
		Integer[] integers = getIds(list, function);
		
		Integer[] mm = ArrayUtil.getMaxAndMinValue(integers);
		
		CustomerPoolSearchParam.setMin(mm[0]);
		
		CustomerPoolSearchParam.setMax(mm[1]);
		
		CustomerPoolMapper CustomerPoolMapper = this.getBaseMapper();
		
		List<CustomerPool> CustomerPools = CustomerPoolMapper.getDataByListBySearch(CustomerPoolSearchParam);
		
		return new PageResponseData<>(CustomerPools.size(), entityConvertToVo(CustomerPools, target));	
	}

	/**
	 * 检查数据
	 * 
	 * @param data
	 * @return
	 */
	protected String checkData(CustomerPool data) {
		
		// 删除缓存
		this.redisTemplate.delete(RedisConstant.getCustomerSeaKey(data.getId()));
		
		return "";
	}


	@Override
	public int delete(int id) {
		// 删除缓存
		this.redisTemplate.delete(RedisConstant.getCustomerSeaKey(id));
		return this.getBaseMapper().deleteByPrimaryKey(id);
	}
	
	
	@Override
	public <VO> VO getCustomerById(IIdParam iIdParam, Class<VO> target) {
		
		CustomerPool customerPool = this.getCustomerByIdToCache(iIdParam);
		
		return CopyEntity.copyEntity(customerPool, target);
	}
	
	


	@Override
	public CustomerPool getCustomerByIdToCache(IIdParam iIdParam) {
		
		int id = iIdParam.getId();
		
		String key = RedisConstant.getCustomerSeaKey(id);
		
		ValueOperations<String, Object> valueOperations = this.redisTemplate.opsForValue();
		
		CustomerPool customerPool = (CustomerPool) valueOperations.get(key);
		
		if (null != customerPool) {
			return customerPool;
		}
		
		customerPool = this.selectByPrimaryKey(iIdParam.getId());
		
		if (null == customerPool) {
			customerPool = new CustomerPool();
			
			valueOperations.set(key, customerPool, 3, TimeUnit.SECONDS);
			
			return customerPool;
		}
		
		valueOperations.set(key, customerPool, 9, TimeUnit.SECONDS);
		
		return customerPool;
	}

}
